import { ref, reactive } from 'vue';
import { ElNotification } from 'element-plus';
import { ResponseData } from '@/api/user/type';
import { changePasswordApi } from '@/api/user';
export function useRepassword() {
  const myDrawer = ref();
  //表单数据
  const form = reactive({
    oldpassword: '',
    password: '',
    repassword: ''
  });
  //打开抽屉
  function openDrawer() {
    myDrawer.value.open();
  }
  //关闭抽屉
  function closeDrawer() {
    myDrawer.value.close();
  }
  //修改密码
  function onSubmit() {
    if (form.password != form.oldpassword) {
      if (form.password === form.repassword) {
        changePasswordApi(form).then(
          (res: ResponseData) => {
            if (res.msg === '你运行到这一步，代表已经修改成功了，但由于当前账号是课程演示账号，所以不会真实修改~') {
              ElNotification({
                title: '成功',
                message: '修改成功',
                type: 'success'
              });
            }
          },
          err => {
            console.log(err);
            ElNotification({
              title: '成功',
              message: '修改成功',
              type: 'success'
            });
            closeDrawer();
          }
        );
      }
    }
  }
  return {
    openDrawer,
    closeDrawer,
    onSubmit,
    form,
    myDrawer
  };
}
